using System;
using System.Collections.Generic;
using ProtoModels;
using UnityEngine;

public class InAppPurchase : ScriptableObject
{
	public enum ItemType
	{
		Coins = 0,
		GiraffeDollars = 1,
		CoinDollarCombo = 2,
		Tokens = 3
	}

	[Serializable]
	public class InAppObject
	{
		public string name;

		public string productId;

		public int getsCoins;

		public int getsDollars;

		public int getsTokens;

		public bool isConsumable;

		public int cost;

		public string currency = "EUR";

		public ItemType type;

		public string styleName;
	}

	protected static InAppPurchase instance_;

	public string base64EncodedPublicKey;

	public List<InAppObject> inAppProducts = new List<InAppObject>();

	protected GGInAppPurchase inApp;

	public static InAppPurchase instance
	{
		get
		{
			if (instance_ == null)
			{
				instance_ = Resources.Load("InAppPurchase", typeof(InAppPurchase)) as InAppPurchase;
				instance_.Init();
			}
			return instance_;
		}
	}

	public InAppObject FindInAppForId(string id)
	{
		foreach (InAppObject inAppProduct in inAppProducts)
		{
			if (inAppProduct.productId == id)
			{
				return inAppProduct;
			}
		}
		return null;
	}

	public void restorePurchases()
	{
		inApp.restorePurchases();
	}

	public void buyProduct(string productId)
	{
		InAppObject inAppObject = FindInAppForId(productId);
		if (inAppObject != null)
		{
			inApp.buy(inAppObject.productId);
		}
	}

	public string GetFormatedPrice(string productId)
	{
		return inApp.GetFormatedPrice(productId);
	}

	public void QueryInventory()
	{
		inApp.QueryInventory();
	}

	protected void Init()
	{
		inApp = GGInAppPurchase.instance;
		inApp.onSetupComplete += OnSetupComplete;
		inApp.onPurchaseComplete += OnProductPurchased;
		List<string> list = new List<string>();
		foreach (InAppObject inAppProduct in inAppProducts)
		{
			if (inAppProduct.isConsumable)
			{
				list.Add(inAppProduct.productId);
			}
		}
		Debug.Log("Base64Key: " + base64EncodedPublicKey);
		inApp.start(list.ToArray(), base64EncodedPublicKey);
	}

	protected void OnSetupComplete(bool success)
	{
		if (success)
		{
			inApp.restorePurchases();
		}
	}

	protected void OnProductPurchased(GGInAppPurchase.PurchaseResponse response)
	{
		string productId = response.productId;
		bool success = response.success;
		Debug.Log("Product " + productId + " success " + success);
		InAppObject inAppObject = null;
		if (!success)
		{
			return;
		}
		foreach (InAppObject inAppProduct in inAppProducts)
		{
			if (inAppProduct.productId == productId)
			{
				GGPlayerSettings gGPlayerSettings = GGPlayerSettings.instance;
				gGPlayerSettings.walletManager.AddCurrency(CurrencyType.coins, inAppProduct.getsCoins);
				gGPlayerSettings.walletManager.AddCurrency(CurrencyType.ggdollars, inAppProduct.getsDollars);
				gGPlayerSettings.walletManager.AddCurrency(CurrencyType.diamonds, inAppProduct.getsTokens);
				PlayerInventory.instance.buyItem(PlayerInventory.Item.NoAds, false);
				Ads.instance.hideBanner(true);
				Analytics.instance.ReportPurchase(inAppProduct);
				inAppObject = inAppProduct;
				GGPlayerSettings.instance.AddPurchase(CreateInAppPurchaseDao(inAppProduct, response));
				break;
			}
		}
	}

	private InAppPurchaseDAO CreateInAppPurchaseDao(InAppObject product, GGInAppPurchase.PurchaseResponse response)
	{
		InAppPurchaseDAO inAppPurchaseDAO = new InAppPurchaseDAO();
		inAppPurchaseDAO.productId = product.productId;
		inAppPurchaseDAO.timeUtc = DateTime.UtcNow.Ticks;
		return inAppPurchaseDAO;
	}

	public List<InAppObject> GetItemsForType(ItemType type)
	{
		List<InAppObject> list = new List<InAppObject>();
		foreach (InAppObject inAppProduct in inAppProducts)
		{
			if (inAppProduct.type == type)
			{
				list.Add(inAppProduct);
			}
		}
		return list;
	}
}
